package com.wanmait.shoppingsys.controller.web;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.shoppingsys.dao.SerachProduitDAO;
import com.wanmait.shoppingsys.dao.TradeDAO;
import com.wanmait.shoppingsys.vo.Trade;
import com.wanmait.shoppingsys.vo.UserInfo;

@WebServlet("/TradeController")

public class TradeController extends HttpServlet{
	
	private TradeDAO tradeDAO=TradeDAO.getDAO();
	private SerachProduitDAO serachProduitDAO=SerachProduitDAO.getDAO();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		if(action==null) return;
		
		switch(action)
		{
		case "updateOrderCount":
			this.updateOrderCount(request, response);
			break;

		case "tradeSession":
			this.tradeSession(request, response);

		case "insert":
			this.insert(request, response);
			break;
			
		}
		
	}
	
	//更新订单 单个商品数量
	protected void updateOrderCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String idStr = request.getParameter("id");
		Integer  id = Integer.parseInt(idStr);
		
		ArrayList<Trade> trades=(ArrayList<Trade>)tradeDAO.findall();
		
		request.setAttribute("trades", trades);
		//更新数量
		tradeDAO.updateOrderCount(id);
		//重定向
		response.sendRedirect("/ShoppingSys/SpecialtyController?action=detailed&id="+id);
	}
	

	//session方法
	protected void tradeSession (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		
		UserInfo userinfo = (UserInfo)request.getSession().getAttribute("userinfo");
		if(userinfo==null) {
			return;
		}
		System.out.println(userinfo);
		
		Integer userinfoId = userinfo.getId();
		String specialtyIdStr=request.getParameter("specialtyId");
		Integer specialtyId=Integer.parseInt(specialtyIdStr);
		String infoStr = request.getParameter("info");
		Integer info =Integer.parseInt(infoStr);
		
		Trade trade=new Trade();
		trade.setSpecialtyId(specialtyId);
		trade.setOrderCount(info);
		
		
		System.out.println(trade);
		request.getRequestDispatcher("/order/order.jsp").forward(request, response);
	}
	
	
	protected void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String specialtyIdStr = request.getParameter("specialtyId");
		Integer  specialtyId = Integer.parseInt(specialtyIdStr);
		String order_countStr = request.getParameter("order_count");
		String order_priceStr = request.getParameter("order_price");
		Integer order_count = Integer.parseInt(order_countStr);
		double order_price = Double.parseDouble(order_priceStr);
		Trade trade = new Trade();
		trade.setOrderCount(order_count);
		trade.setOrderPrice(order_price);
		trade.setSpecialtyId(specialtyId);
		trade.setOrderId(1);
		tradeDAO.insert(trade);

	}
	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
